package com.techsoft.dataconverter;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import com.techsoft.DataConverter;

public class DataConverterFactory {
	private static List<String> dataformats = new CopyOnWriteArrayList<String>();
	private static Map<String, String> converterclasses = new ConcurrentHashMap<String, String>();

	static {
		DataConverterFactory.registerDataFormatName("json");
		DataConverterFactory.registerDataFormatName("jsonp");
		DataConverterFactory.registerDataFormatName("xml");
		DataConverterFactory.registerDataFormatName("amf3");
		DataConverterFactory.registerDataFormatName("java");
		DataConverterFactory.registerDataFormatName("delphixml");

		try {
			DataConverterFactory.registerConverter("json",
					"com.techsoft.dataconverter.JSONConverter");
			DataConverterFactory.registerConverter("jsonp",
					"com.techsoft.dataconverter.JSONPConverter");
			DataConverterFactory.registerConverter("xml",
					"com.techsoft.dataconverter.XMLConverter");
			DataConverterFactory.registerConverter("amf3",
					"com.techsoft.dataconverter.AMF3Converter");
			DataConverterFactory.registerConverter("java",
					"com.techsoft.dataconverter.JAVAConverter");
			DataConverterFactory.registerConverter("delphixml",
					"com.techsoft.dataconverter.DELPHIXMLConverter");

		} catch (Exception e) {

		}
	}

	public static boolean containDataFormat(String dataformat) {
		return dataformats.contains(dataformat.toLowerCase());
	}

	public static void registerDataFormatName(String dataformat) {
		if (!dataformats.contains(dataformat.toLowerCase())) {
			dataformats.add(dataformat.toLowerCase());
		}
	}

	public static void unRegisterDataFormatName(String dataformat) {
		if (dataformats.contains(dataformat.toLowerCase())) {
			dataformats.remove(dataformat.toLowerCase());
		}
	}

	public static void registerConverter(String dataformat,
			String converterClass) throws Exception {
		if (dataformats.contains(dataformat.toLowerCase())) {
			converterclasses.put(dataformat.toLowerCase(), converterClass);
		} else {
			throw new Exception("请先注册数据格式名称后， 再注册数据转换器!");
		}
	}

	public static void unRegisterConverter(String dataFormatName,
			String converterClass) throws Exception {
		if (dataformats.contains(dataFormatName.toLowerCase())) {
			converterclasses.remove(dataFormatName.toLowerCase());
		}
	}

	public static DataConverter getSerialization(String dataformat) {
		String converterclass = converterclasses.get(dataformat.toLowerCase());
		DataConverter result = null;
		try {
			if (converterclass != null) {
				try {
					Class<?> cls = Class.forName(converterclass);
					result = (DataConverter) cls.newInstance();
				} catch (Exception e) {
					throw new Exception("不能创建 DataConverter[ " + converterclass
							+ " ], 错误信息为: " + e.getMessage());
				}
			} else {
				throw new Exception("没有实现[ " + dataformat + " ]数据的序列化实现");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
